<?php
class ModelLocalisationLanguage extends Model {
	
	public function getLanguages($data = array()) {
		if($data) {
			$sql = "SELECT * FROM " . DB_PREFIX . 'language';
			
			$sort_data = array(
				'name',
				'code',
				'sort_order'
			);
			
			if(isset($data['sort']) && in_array($data['sort'], $sort_data)) {
				$sql .= " ORDER BY " . $data['sort'];
			} else {
				$sql .= " ORDER BY sort_order, name";
			}
			
			if(isset($data['order']) && ($data['order'] == 'DESC')) {
				$sql .= " DESC";
			} else {
				$sql .= " ASC";
			}
			
			if(isset($data['start']) || isset($data['limit'])) {
				if($data['start'] < 0){
					$data['start'] = 0;
				}
				
				if($data['limit'] < 1){
					$data['limit'] = 20;
				}
			}
			
			$sql .= " LIMIT " . (int)$data['start'] . ',' . (int)$data['limit'];
			
			$query = $this->db->query($sql);
			return $query->rows;
		} else {
			$language_data = $this->cache->get('language');
			if(!$language_data) {
				$language_data = array();
				
				$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "language ORDER BY sort_order, name");
				
				foreach ($query->rows as $result) {
					$language_data[$result['code']] = array(
						'language_id'	=> $result['language_id'],
						'name'			=> $result['name'],
						'code'			=> $result['code'],
						'locale'		=> $result['locale'],
						'image'			=> $result['image'],
						'directory'		=> $result['directory'],
						'filename'		=> $result['filename'],
						'sort_order'	=> $result['sort_order'],
						'status'		=> $result['status']
					);
					
					$this->cache->set('language', $language_data);
				}
			}
			return $language_data;
		}
	}
}